package com.ct.gd.passport.domain;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.ct.gd.passport.common.connect.CtServer;
import com.ct.gd.passport.common.constants.Config;
import com.ct.gd.passport.common.constants.Protocol;
import com.fuckhtc.gson.Gson;
import com.fuckhtc.gson.annotations.SerializedName;
import com.fuckhtc.gson.reflect.TypeToken;
import com.up91.common.android.connect.Params;
import com.up91.common.android.exception.NullDataException;

public class PolicyCatalog {
	@SerializedName("id")
	private int		catalogId;
	
	@SerializedName("name")
	private String	name;

	public int getCatalogId() {
		return catalogId;
	}

	public void setCatalogId(int catalogId) {
		this.catalogId = catalogId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static List<PolicyCatalog> loadRootCatalog() {
		List<PolicyCatalog> infos = new ArrayList<PolicyCatalog>();
		PolicyCatalog info = null;
		info = new PolicyCatalog();
		info.setCatalogId(1);
		info.setName("基本常识");
		infos.add(info);

		info = new PolicyCatalog();
		info.setCatalogId(2);
		info.setName("出境问题");
		infos.add(info);

		info = new PolicyCatalog();
		info.setCatalogId(3);
		info.setName("入境问题");
		infos.add(info);
		return infos;
	}

	public static List<PolicyCatalog> loadCatalogs(int type) {
		Params params = new Params();
		params.put(Protocol.Fields.POLICY_TYPE, type);

		if (Config.ON_LINE) {
			String json = CtServer.getServer().doGet(Protocol.Commands.POLICY_LIST, params);
			try {
				JSONObject jObj = new JSONObject(json);
				
				TypeToken<List<PolicyCatalog>> typeToken = new TypeToken<List<PolicyCatalog>>() {
				};
				return new Gson().fromJson(jObj.getString(Protocol.Fields.DATA) , typeToken.getType());
			} catch (JSONException e) {
				e.printStackTrace();
				throw new NullDataException();
			}
			
		}
		String catalogName = (type == 1) ? "基本常识" : "出入境问题";

		List<PolicyCatalog> infos = new ArrayList<PolicyCatalog>();
		PolicyCatalog info = null;
		for (int i = 100; i < 110; i++) {
			info = new PolicyCatalog();
			info.setCatalogId(i);
			info.setName("第" + i + "条" + catalogName);
			infos.add(info);
		}
		return infos;
	}

	public static List<PolicyCatalog> searchCatalog(String keyword) {
		Params params = new Params();
		params.put(Protocol.Fields.POLICY_SEARCH_KEYWORD, keyword);

		if (Config.ON_LINE) {
			String json = CtServer.getServer().doGet(Protocol.Commands.POLICY_SEARCH, params);
			try {
				JSONObject jObj = new JSONObject(json);
				
				TypeToken<List<PolicyCatalog>> typeToken = new TypeToken<List<PolicyCatalog>>() {
				};
				return new Gson().fromJson(jObj.getString(Protocol.Fields.DATA) , typeToken.getType());
			} catch (JSONException e) {
				e.printStackTrace();
				throw new NullDataException();
			}
		}

		List<PolicyCatalog> infos = new ArrayList<PolicyCatalog>();
		PolicyCatalog info = null;
		for (int i = 100; i < 110; i++) {
			info = new PolicyCatalog();
			info.setCatalogId(i);
			info.setName("第" + i + "条" + "办理政策");
			infos.add(info);
		}
		return infos;
	}

}
